﻿function AnalyseError(container, config, titlename) {
    this.timepoints = [];
    this.absoluteValueInForecastModel0 = [];
    this.absoluteValueInForecastModel1 = [];
    this.absoluteValueInForecastModel2 = [];
    this.absoluteValueInForecastModel3 = [];
    this.absoluteValueInForecastModel4 = [];
    this.absoluteValueInForecastModel5 = [];
    this.forTime = [];
    this.forecastScheme = [];
    this.forecastValueInForecastModel0 = [];
    this.forecastValueInForecastModel1 = [];
    this.forecastValueInForecastModel2 = [];
    this.forecastValueInForecastModel3 = [];
    this.forecastValueInForecastModel4 = [];
    this.forecastValueInForecastModel5 = [];
    this.monitorValue = [];
    this.pollutantName = [];
    this.relativeValueInInForecastModel0 = [];
    this.relativeValueInInForecastModel1 = [];
    this.relativeValueInInForecastModel2 = [];
    this.relativeValueInInForecastModel3 = [];
    this.relativeValueInInForecastModel4 = [];
    this.relativeValueInInForecastModel5 = [];
    this.series = [];
    this.seriesParam = config["seriesParam"] || [];
    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;
    this.stationName = config["stationName"] || "";
    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];
    this.audit = config["audit"] || false;
    this.auditUrl = config["auditUrl"] || null;
    this.recordUrl = config["recordUrl"] || null;
    this.people = config["people"] || false;
    this.peopleLang = "预报员的预报值";

    this.container = container;
    this.titlename = titlename;
}

AnalyseError.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table>';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td>' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;


};


AnalyseError.prototype.titleFormatter = function () {
    var _ = this;
    return _.stationName + pollutantNameSubTransform(_.titlename) + '对比分析图' || '对比分析图';
}

AnalyseError.prototype.getStep = function () {
    var _ = this,
        width = $("#" + _.container).width(),
        count = _.timepoints.length,
        num = 1,
        xWidth = _.xwidth;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

AnalyseError.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        lang: {
            contextButtonTitle: '导出图片',
            loading: '加载中',
            noData: '没有数据'
        },
        title: {
            useHTML: true,
            text: _.titleFormatter()
        },
        credits: {
            enabled: false
        },
        xAxis: [{
            categories: _.forTime,
            tickInterval: _.getStep()
        }],
        yAxis: {
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            }
        },
        tooltip: {
            shared: true,
            crosshairs: true,
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        credits: {
            enabled: false
        },
        series: _.series
    }
};

AnalyseError.prototype.onChartLoad = function () {
};

AnalyseError.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

AnalyseError.prototype.initData = function (jsonData, pollutantName) {
    var _ = this;
    var nameAbsoluterError = pollutantName + "_AbsoluterError"
    if (jsonData.length > 0) {
        var d = jsonData;
        for (var i = 0; i < d.length; i++) {
            if (d[i].ForecastModelId == 0) {
                _.absoluteValueInForecastModel0.push(d[i][nameAbsoluterError]);
            }
            if (d[i].ForecastModelId == 1) {
                _.absoluteValueInForecastModel1.push(d[i][nameAbsoluterError]);
            }
            if (d[i].ForecastModelId == 2) {
                _.absoluteValueInForecastModel2.push(d[i][nameAbsoluterError]);
            }
            if (d[i].ForecastModelId == 3) {
                _.absoluteValueInForecastModel3.push(d[i][nameAbsoluterError]);
            }
            if (d[i].ForecastModelId == 5) {
                _.absoluteValueInForecastModel5.push(d[i][nameAbsoluterError]);
            }
            var time = _.formatDate(d[i].ForTime);
            if (_.forTime[time] == undefined) {
                _.forTime.push(time);
            }
        }
        _.series = _.getSeries(_.series);
        _.createChart();
    }
    else {
        return "no data";
    }





    //var _ = this;
    //if (jsonData.length > 0) {
    //    var d = jsonData;
    //    for (var i = 0; i < jsonData.length; i++) {
    //        _.absoluteValueInForecastModel0.push(d[i].AbsoluteValueInForecastModel0);
    //        _.absoluteValueInForecastModel1.push(d[i].AbsoluteValueInForecastModel1);
    //        _.absoluteValueInForecastModel2.push(d[i].AbsoluteValueInForecastModel2);
    //        _.absoluteValueInForecastModel3.push(d[i].AbsoluteValueInForecastModel3);
    //        _.absoluteValueInForecastModel4.push(d[i].AbsoluteValueInForecastModel4);
    //        _.absoluteValueInForecastModel5.push(d[i].AbsoluteValueInForecastModel5);
    //        _.forTime.push(_.formatDate(_.GetTimeFromJsonString(d[i].ForTime), "MM月DD日"));
    //        _.forecastScheme.push(d[i].ForecastScheme);

    //        _.forecastValueInForecastModel0.push(d[i].ForecastValueInForecastModel0);
    //        _.forecastValueInForecastModel1.push(d[i].ForecastValueInForecastModel1);
    //        _.forecastValueInForecastModel2.push(d[i].ForecastValueInForecastModel2);
    //        _.forecastValueInForecastModel3.push(d[i].ForecastValueInForecastModel3);
    //        _.forecastValueInForecastModel4.push(d[i].ForecastValueInForecastModel4);
    //        _.forecastValueInForecastModel5.push(d[i].ForecastValueInForecastModel5);

    //        _.monitorValue.push(d[i].MonitorValue);
    //        _.pollutantName.push(d[i].PollutantName);
    //        _.relativeValueInInForecastModel0.push(d[i].RelativeValueInInForecastModel0);
    //        _.relativeValueInInForecastModel1.push(d[i].RelativeValueInInForecastModel1);
    //        _.relativeValueInInForecastModel2.push(d[i].RelativeValueInInForecastModel2);
    //        _.relativeValueInInForecastModel3.push(d[i].RelativeValueInInForecastModel3);
    //        _.relativeValueInInForecastModel4.push(d[i].RelativeValueInInForecastModel4);
    //        _.relativeValueInInForecastModel5.push(d[i].RelativeValueInInForecastModel5);
    //        _.timepoints.push(_.formatDate(_.GetTimeFromJsonString(d[i].TimePoint), "MM月DD日"));
    //    }
    //    _.series = _.getSeries(_.series);
    //    _.createChart();
    //}
    //else {
    //    return "no data";
    //}
};

AnalyseError.prototype.getSeries = function (series, people) {
    var _ = this;

    series = [];


    series.push({
        name: '多模式择优绝对误差',
        color: '#9900CC',
        type: 'column',
        data: _.absoluteValueInForecastModel0
    });

    series.push({
        name: '多元回归绝对误差',
        color: '#7CB5EC',
        type: 'column',
        data: _.absoluteValueInForecastModel1
    });

    series.push({
        name: '同期回归绝对误差',
        color: '#90ED7D',
        type: 'column',
        data: _.absoluteValueInForecastModel2
    });

    series.push({
        name: '神经网络绝对误差',
        color: '#F45B5B',
        type: 'column',
        data: _.absoluteValueInForecastModel3
    });

    series.push({
        name: '聚类回归绝对误差',
        color: '#D3BA77',
        type: 'column',
        data: _.absoluteValueInForecastModel5
    });

    return series;
}

AnalyseError.prototype.formatDate = function (date, format) {
    if (arguments.length < 2 && !date.getTime) {
        format = date;
        date = new Date();
    }
    typeof format != 'string' && (format = 'YYYY年MM月DD日 hh时mm分ss秒');
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
    return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function (a) {
        switch (a) {
            case "YYYY": return date.getFullYear();
            case "YY": return (date.getFullYear() + "").slice(2);
            case "MM": return date.getMonth() + 1;
            case "DD": return date.getDate();
            case "hh": return date.getHours();
            case "mm": return date.getMinutes();
            case "ss": return date.getSeconds();
            case "星期": return "星期" + week[date.getDay() + 7];
            case "周": return "周" + week[date.getDay() + 7];
            case "week": return week[date.getDay()];
            case "www": return week[date.getDay()].slice(0, 3);
        }
    });
}

//将json时间转换成javascript时间

AnalyseError.prototype.GetTimeFromJsonString = function (jsonTime) {
    var re = /-?\d+/;
    var m = re.exec(jsonTime);
    var d = new Date(parseInt(m[0]));
    return d;
}